"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const adm_zip_1 = __importDefault(require("adm-zip"));
const path_1 = __importDefault(require("path"));
const getServerInfo_1 = __importDefault(require("../../src/lib/System/getServerInfo"));
const FSTool_1 = __importDefault(require("../../src/lib/FSTool"));
async function installDependencies() {
    let inPkgEXE = __dirname.includes('snapshot');
    const serverInfo = await (0, getServerInfo_1.default)();
    const npmAssets = [
        { name: 'sqlite3', path: 'sqlite3-5.17' }
    ];
    for (let i = 0; inPkgEXE && i < npmAssets.length; i++) {
        let appBasePath = path_1.default.dirname(process.execPath);
        let insidePath = path_1.default.join(__dirname, '../../');
        const npmName = npmAssets[i].name;
        const npmPath = npmAssets[i].path;
        const sysDevDependenciesPath = path_1.default.join(appBasePath, `node_modules/${npmName}`);
        let npmZipPath;
        if (serverInfo.operatingSystem === 'Windows_NT') {
            npmZipPath = path_1.default.join(insidePath, `assets/devDependencies/${npmPath}/win-x86/${npmName}.zip`);
        }
        else if (serverInfo.operatingSystem === 'Linux' && serverInfo.arch === 'x64') {
            npmZipPath = path_1.default.join(insidePath, `assets/devDependencies/${npmPath}/linux-x86/${npmName}.zip`);
        }
        else if (serverInfo.operatingSystem === 'Linux' && serverInfo.arch === 'arm64') {
            npmZipPath = path_1.default.join(insidePath, `assets/devDependencies/${npmPath}/linux-arm64/${npmName}.zip`);
        }
        fs_extra_1.default.ensureDirSync(sysDevDependenciesPath);
        const zip = new adm_zip_1.default(npmZipPath);
        const zipEntries = zip.getEntries();
        const zipFileList = zipEntries
            .map((zipEntry) => JSON.parse(zipEntry.toString()))
            .filter((zipFile) => !zipFile.isDirectory);
        if (FSTool_1.default.getFilePathList(sysDevDependenciesPath).length !== zipFileList.length) {
            console.log(`系统未安装 ${npmName} 依赖组件，正在安装中...`);
            fs_extra_1.default.removeSync(sysDevDependenciesPath);
            zip.extractAllTo(sysDevDependenciesPath, true);
            console.log(`${npmName} 依赖组件，安装完成`);
        }
    }
}
exports.default = installDependencies;
